<?php
/***************************************************************************
 *   Copyright (C) 2006 by Fernando Lagos <fernando@zerial.org> 	   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

class MySQL{
	public $socket;
	function __construct($_username, $_password, $_hostname, $_port = 3306){
		$this->socket = mysql_connect($_hostname.":".$_port, $_username, $_password);
	}
	function database($_dbname){
		mysql_select_db($_dbname);
	}
	function search($_pattern, &$object){
		$sql_where = str_replace("{CONDITION}", "AND", MySQL::_sqlwhere($_pattern));
		$_query = mysql_query("SELECT * FROM ".$object->table_name." WHERE ".$sql_where, $this->socket);
		$object->result = mysql_fetch_array($_query, MYSQL_ASSOC);
		return mysql_num_rows($_query);
	}
	function query($_query){
		return mysql_query($_query);
	}
	function loadData(&$object, $_limit = NULL){
		$_arr = array();
		if($_query = mysql_query("SELECT * FROM ".$object->table_name.(($_limit)?" LIMIT ".$_limit:""), $this->socket)){
			if(mysql_num_rows($_query) > 0){
				while($_row = mysql_fetch_assoc($_query)){
					foreach(array_keys($_row) as $_key){
						$_arr[$_key][] = $_row[$_key];
					}
				}
				$this->setValue($object, $_arr);
			}
		}
	}

	function _sqlwhere($_pattern){
		$where = "";
		foreach(array_keys($_pattern) as $_key){
			$where .= $_key." = '".$_pattern[$_key]."' {CONDITION} ";
		}
		return substr($where, -strlen($where), strlen($where)-13);
	}
	function loadModel(&$object){
		$_query = mysql_query("SHOW TABLES", $this->socket);
		if(mysql_num_rows($_query) > 0){
			while($_row = mysql_fetch_row($_query)){
				$object->$_row[0] = new SapormTable($this);
				$__query = mysql_query("SHOW COLUMNS FROM ".$_row[0], $this->socket);
				$object->$_row[0]->table_name = $_row[0];
				while($_field = mysql_fetch_row($__query))
					$object->$_row[0]->$_field[0] = array();
			}
		}
	}
	function setValue(&$object, $_values = array()){
		foreach(array_keys($_values) as $value){
			foreach($_values[$value] as $__value){
				array_push($object->$value, $__value);
			}
		}
	}

}
?>
